﻿using Hrm.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hrm.Services
{
    public class BlogService : BaseService<BlogPost>, IBlogService
    {
        IBlogPostStudioDbRepository _blogPostStudioDbRepository;
        IBlogCommentStudioDbRepository _blogCommentStudioDbRepository;
        IBlogTagStudioDbRepository _blogTagStudioDbRepository;
        public BlogService(IBlogPostStudioDbRepository blogPostStudioDbRepository, IBlogCommentStudioDbRepository blogCommentStudioDbRepository, IBlogTagStudioDbRepository blogTagStudioDbRepository)
            : base(blogPostStudioDbRepository)
        {
            _blogPostStudioDbRepository = blogPostStudioDbRepository;
            _blogCommentStudioDbRepository = blogCommentStudioDbRepository;
            _blogTagStudioDbRepository = blogTagStudioDbRepository;
        }

        public PageResponse<BlogPost> PageBlogPostList(PageRequest<BlogPost> request)
        {
            var query = this.GetAllAbledAndOrderByCreatetime();
            if (!string.IsNullOrEmpty(request.filter.Tags))
            {
                var tags = request.filter.Tags.Split(',');
                query = query.Where(p => tags.Any(x => p.Tags.Contains(x) ));
            }
            PageResponse<BlogPost> result = new PageResponse<BlogPost>(query, request.draw, request.start, request.length);
            return result;
        }

        public List<BlogPost> PopularBlogPostList()
        {
            var query = this.GetAllAbledAndOrderByCreatetime();
            query = query.OrderByDescending(p => p.BlogComments.Count()).Take(10);
            return query.ToList();
        }

        public List<BlogPost> TopBlogPostList(int topNum)
        {
            var query = this.GetAllAbledAndOrderByCreatetime();
            query = query.Take(topNum);
            return query.ToList();
        }

        public List<BlogPost> RecentBlogPostList()
        {
            var query = this.GetAllAbledAndOrderByCreatetime();
            query = query.Take(10);
            return query.ToList();
        }

        public List<BlogComment> RecentBlogCommentList()
        {
            var query = _blogCommentStudioDbRepository.GetAll().OrderByDescending(p => p.CreateDatetime).AsQueryable();
            query = query.Take(10);
            return query.ToList();
        }


        public List<BlogTag> GetAllTag()
        {
            var query = this._blogTagStudioDbRepository.GetList(p => p.IsEnable).OrderByDescending(p => p.CreateDatetime);
            return query.ToList();
        }

        public bool AddComment(BlogComment model)
        {
            model.CreateDatetime = DateTime.Now;
            return this._blogCommentStudioDbRepository.Insert(model)>0;
        }

        public bool DeleteComment(int id)
        {
            var model = this._blogCommentStudioDbRepository.GetById(id);
            return this._blogCommentStudioDbRepository.Delete(model) > 0;
        }

        public BlogPost PreBlogPost(int id)
        {
            var temp = id;
            BlogPost result = null;
            while (temp > 0)
            {
                temp = temp - 1;
                result = this.GetById(temp);
                if (result != null && result.IsEnable)
                    break;
            }
            return result;
        }

        public BlogPost NextBlogPost(int id)
        {
            var temp = id;
            BlogPost result = null;
            var maxId = this.GetList(p => p.IsEnable).Max(p => p.Id);
            while (temp <= maxId)
            {
                temp = temp + 1;
                result = this.GetById(temp);
                if (result != null && result.IsEnable)
                    break;
            }
            return result;
        }
    }
}
